<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="mini" v-show="showSearch" label-width="68px">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="姓名" prop="staffName">
            <el-input v-model="queryParams.staffName" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table v-loading="loading" :data="staffList" @selection-change="handleSelectionChange" border>
      <el-table-column label="员工编号" align="center" prop="staffId" width="80px;"/>
      <el-table-column label="员工姓名" align="center" prop="staffName" width="150px;"/>
      <el-table-column label="性别" align="center" prop="staffSex" width="80px;">
        <template slot-scope="scope">
          {{ formatGender(scope.row.staffSex) }}
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" width="80px;">
        <template slot-scope="scope">
          {{ calculateAge(scope.row.staffIdcard) }}
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center" prop="staffIdcard" width="200px;"/>
      <el-table-column label="联系方式" align="center" prop="staffPhone" width="150px;"/>
      <el-table-column label="职别" align="center" prop="jobName" width="100px;"/>
      <el-table-column label="银行卡" align="center" prop="bankCard" width="200px;"/>
      <el-table-column label="家庭住址" align="center" prop="staffAddress" />
      <!--<el-table-column label="推荐人" align="center" prop="recommenStaffId" width="150px;"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200px;">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleReinstatement(scope.row)"
            v-hasPermi="['cld:staff:reinstatement']">恢复已入职</el-button>
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
            v-hasPermi="['cld:staff:query']">查看</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)"
            v-hasPermi="['cld:staff:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />


        <!-- 修改人员 -->
    <el-dialog :close-on-click-modal="false" :destroy-on-close="true" :visible.sync="open" append-to-body title="个人信息"
               width="50%">
      <add-staff-vue v-if="open" ref="addStaff" :staffId="staffId" ></add-staff-vue>
    </el-dialog>
  </div>
</template>

<script>
import { listStaff, getStaff, delStaff, addStaff, reinstatementStaff } from "@/api/cld/staff";
import addStaffVue from "./addStaff.vue";
export default {
  name: "Staff",
  dicts: ['cld_staff_levels'],
  inject: ['formatGender'],
  components: {
    addStaffVue
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员花名册表格数据
      staffList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        staffName: null,
        staffStatus: 4
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询人员花名册列表 */
    getList() {
      this.loading = true;
      listStaff(this.queryParams).then(response => {
        this.staffList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 修改员工
    handleUpdate(row) {
      this.staffId = row.staffId;
      this.open = true;
    },
    // 恢复已入职
    handleReinstatement(row) {
      this.$modal.confirm('是否将员工姓名为"' + row.staffName + '"恢复为已入职？').then(() => {
        reinstatementStaff(row.staffId).then(res => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        });
      }).catch(() => { });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除员工姓名为"' + row.staffName + '"的数据项？').then(function () {
        return delStaff(row.staffId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    handleSelectionChange() {}
  }
};
</script>
